package com.ly.os;

import java.util.ArrayList;

/**
 * @Classname Brank
 * @Description TODO
 * @Date 2020/12/7 13:47
 * @Author 冷心影翼
 */
public class Brank {

	private int[][] max;

	private int[][] allocation;

	private int[][] need;

	private boolean[] check ;

	private int[] available;

	public ArrayList<Integer> safe = new ArrayList<>();

	public Brank(int[][] max, int[][] allocation, int[][] need, int[] available) {
		this.max = max;
		this.allocation = allocation;
		this.need = need;
		this.available = available;
		check = new boolean[max.length];
	}

	public void allocation() {
		while (true) {
			boolean isSafe = false;
			int index = 0;
			for(int[] current : need) {
				boolean flag = true;
				for(int i=0;i<current.length;i++) {
					if(available[i] < current[i]) {
						flag = false;
						break;
					}
				}
				if(flag && !check[index]) {
					for(int i=0;i<current.length;i++) {
						available[i] += allocation[index][i];
					}
					check[index] = true;
					safe.add(index);
					isSafe = true;
				}
				index++;
			}
			if(!isSafe) {
				break;
			}
		}

	}

	public static void main(String[] args) {
		int[][] max = new int[][] {
			{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}
		};
		int[][] allocation = new int[][] {
			{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}
		};
		int[][] need = new int[][] {
			{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}
		};
		int[] available = new int[] {3,3,2};
		Brank brank = new Brank(max,allocation,need,available);
		brank.allocation();
		brank.safe.forEach(System.out::println);
	}
}
